<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-data">&nbsp;&nbsp;财务管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-notebook-1">&nbsp;&nbsp;工资核算</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
      <el-form ref="form" :model="empForm" style="margin-left: 20px">
        <el-form-item class="emp_formSubmit_search">
          <el-select v-model="empForm.month" placeholder="请选择月份"
                     @change="loadEmpNList()">
            <el-option
                v-for="item in MonthOptions"
                :key="item.id"
                :label="item.month"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table
          :data="empSalary.slice((currentPage-1)*pageSize,currentPage*pageSize)"
          stripe
          style="width: 100%">
        <el-table-column prop="month" label="月份" width="200px"></el-table-column>
        <el-table-column prop="employeeId"label="职员ID" width="200px"></el-table-column>
        <el-table-column prop="name" label="职员姓名" width="200px"></el-table-column>
        <el-table-column prop="gender" label="性别" width="200px"></el-table-column>
        <el-table-column prop="hiredate" label="入职时间" width="200px"></el-table-column>
        <el-table-column prop="deptName" label="部门名称" width="200px"></el-table-column>
        <el-table-column prop="job" label="工作职位" width="200px"></el-table-column>
        <el-table-column prop="compensatoryDays" label="调休天数" width="200px"></el-table-column>
        <el-table-column prop="attendanceDays" label="出勤天数" width="200px"></el-table-column>
        <el-table-column prop="baseFunds" label="基本工资" width="200px"></el-table-column>
        <el-table-column prop="postFunds" label="岗位工资" width="200px"></el-table-column>
        <el-table-column prop="subsidy" label="津贴" width="200px"></el-table-column>
        <el-table-column prop="publicAccumulationFunds" label="公积金" width="200px"></el-table-column>
        <el-table-column prop="jobLifeFunds" label="工龄工资" width="200px"></el-table-column>
        <el-table-column prop="lessFunds" label="工资克扣" width="200px"></el-table-column>
        <el-table-column prop="remarks" label="克扣备注" width="200px"></el-table-column>
        <el-table-column prop="sumSalary" label="工资合计" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="empSalary.length">
        </el-pagination>
      </div>
      
      <!-- 以下是弹出的修改人员信息的对话框 -->
      <el-dialog
          width="80%"
          :modal-append-to-body="false"
          title="编辑人员信息"
          :visible.sync="dialogFormVisible">
        <el-form
            :model="empForm"
            :rules="rules"
            ref="empForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="月份:" prop="month">
                <el-select v-model="empForm.month" placeholder="请选择月份">
                  <el-option
                      v-for="item in MonthOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="姓名:" prop="name">
                <el-input v-model="empForm.name" placeholder="输入姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="性别:" prop="gender">
                <el-select v-model="empForm.gender" placeholder="请选择性别">
                  <el-option
                      v-for="item in genders"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="职员ID:" prop="employeeId">
                <el-input v-model="empForm.employeeId" placeholder="输入职员ID"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="入职时间:" prop="hiredate">
                <el-date-picker
                    v-model="empForm.hiredate"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择入职时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门名称:" prop="deptName">
                <el-input v-model="empForm.deptName" placeholder="请输入部门名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工作职位:" prop="job">
                <el-input v-model="empForm.job" placeholder="请输入工作职位"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="调休天数:" prop="compensatoryDays">
                <el-input v-model="empForm.compensatoryDays" placeholder="请输入调休天数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出勤天数:" prop="attendanceDays">
                <el-input v-model="empForm.attendanceDays" placeholder="请输入出勤天数"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="基本工资:" prop="baseFunds">
                <el-input v-model="empForm.baseFunds" placeholder="请输入基本工资"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="岗位工资:" prop="postFunds">
                <el-input v-model="empForm.postFunds" placeholder="请输入岗位工资"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="津贴:" prop="subsidy">
                <el-input v-model="empForm.subsidy" placeholder="请输入津贴"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="公积金:" prop="publicAccumulationFunds">
                <el-input v-model="empForm.publicAccumulationFunds" placeholder="请输入公积金"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工龄工资:" prop="jobLifeFunds">
                <el-input v-model="empForm.jobLifeFunds" placeholder="请输入工龄工资"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工资克扣:" prop="lessFunds">
                <el-input v-model="empForm.lessFunds" placeholder="请输入工资克扣"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="克扣备注:" prop="remarks">
            <el-input v-model="empForm.remarks" placeholder="输入克扣备注"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="20">
              <el-form-item label="工资合计:" prop="sumSalary">
                <el-input v-model="empForm.sumSalary" placeholder="输入工资合计"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary"@click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button @click="dialogFormVisible = false" type="warning" style="color:white;">取 消</el-button>
            <el-button type="primary" @click="submitEditForm()" style="color:white;">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible:false,
      genders:[
        {label:'男',value:'男'},
        {label:'女',value:'女'},
      ],
      MonthOptions: [
        {label: '一月', value: '一月'},
        {label: '二月', value: '二月'},
        {label: '三月', value: '三月'},
        {label: '四月', value: '四月'},
        {label: '五月', value: '五月'},
        {label: '六月', value: '六月'},
        {label: '七月', value: '七月'},
        {label: '八月', value: '八月'},
        {label: '九月', value: '九月'},
        {label: '十月', value: '十月'},
        {label: '十一月', value: '十一月'},
        {label: '十二月', value: '十二月'},
      ],
      empForm: {
        id:'',
        month: '',
        employeeId: '',
        name: '',
        gender: '',
        hiredate: '',
        deptName: '',
        job: '',
        compensatoryDays: '',
        attendanceDays: '',
        baseFunds: '',
        postFunds: '',
        subsidy: '',
        publicAccumulationFunds: '',
        jobLifeFunds: '',
        lessFunds: '',
        remarks: '',
        sumSalary: '',
      },
      empSalary: [],
      currentPage: 1,
      pageSize: 10,
      rules: {
        month: [
          {required: true, message: "请选择月份", trigger: 'change'},
        ],
        employeeId: [
          {required: true, message: "请输入职员ID", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        name: [
          {required: true, message: "请输入姓名", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        gender: [
          {required: true, message: "请选择性别", trigger: 'change'},
        ],
        hiredate: [
          {required: true, message: "请选择入职日期", trigger: 'change'},
        ],
        deptName: [
          {required: true, message: "请输入部门名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        job: [
          {required: true, message: "请输入工作职位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        compensatoryDays: [
          {required: true, message: "请输入调休天数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        attendanceDays: [
          {required: true, message: "请输入出勤天数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        baseFunds: [
          {required: true, message: "请输入基本工资", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        postFunds: [
          {required: true, message: "请输入岗位工资", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        subsidy: [
          {required: true, message: "请输入津贴", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        publicAccumulationFunds: [
          {required: true, message: "请输入公积金", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        jobLifeFunds: [
          {required: true, message: "请输入工龄工资", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        lessFunds: [
          {required: true, message: "请输入工资克扣", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        sumSalary: [
          {required: true, message: "请输入工资合计", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        remarks: [
          {required: true, message: "请输入备注", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
      },
    }
  },
  methods: {
    compute() {
      let sumSalary =
          (
              parseInt(this.empForm.baseFunds) +
              parseInt(this.empForm.postFunds) +
              parseInt(this.empForm.subsidy) +
              parseInt(this.empForm.publicAccumulationFunds) +
              parseInt(this.empForm.jobLifeFunds) -
              parseInt(this.empForm.lessFunds)
          ) * (
              parseInt(this.empForm.attendanceDays) -
              parseInt(this.empForm.compensatoryDays)
          ) / 30 ;
      this.$set(this.empForm,'sumSalary',sumSalary)
    },
    openEditDialog(empForm) {
      let url = 'http://localhost:9080/financePayrolls/' + empForm.id ;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.empForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadEmpRollList();
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9080/financePayrolls/' + this.empForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.empForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改人员信息成功！',
            type: 'success'
          });
          this.loadEmpRollList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadEmpRollList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openDeleteConfirm(empForm) {
      let message = '此操作将永久删除【' + empForm.name + '】人员信息，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(empForm);
      }).catch(() => {
      });
    },
    handleDelete(empForm) {
      let url = 'http://localhost:9080/financePayrolls/' + empForm.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除人员信息成功！',
            type: 'success'
          });
          local.reload();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadEmpRollList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadEmpRollList() {
      let url = 'http://localhost:9080/financePayrolls';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('message=' + responseBody.message);
        this.empSalary = responseBody.data;
        console.log('empSalary=' + responseBody.data);
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadEmpRollList();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.emp_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>